home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DDJMAG / DDJ8801.ZIP / NARO.ZIP / READCFG.C < prev    next >
Text File  |  1987-10-30  |  6KB  |  245 lines

  1. /*
  2.     Copyright (C) 1987 Paradigm Systems Inc.  All rights reserved.
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. #include "loc.h"
  10. #include "externs.h"
  11.  
  12. int    process_class_keyword(), process_order_keyword() ;
  13. int    process_rom_keyword(), process_dup_keyword() ;
  14.  
  15.  
  16. static    struct    CFG_COMMANDS   {
  17.             char    *cmd ;
  18.             int    (*command)() ;
  19.         }    cfg_cmds[] = {
  20.                 "CLASS",        process_class_keyword,
  21.                 "ORDER",        process_order_keyword,
  22.                 "ROM",        process_rom_keyword,
  23.                 "DUP",        process_dup_keyword,
  24.             } ;
  25.  
  26. static    unsigned int    line ;
  27.  
  28. int    process_locate_file(seg_list)
  29. SEG_DESCRIPTOR    *seg_list ;
  30. {
  31.     int    i, error = OK;
  32.     char    *tok, *buf ;
  33.  
  34.     /*
  35.         This function reads the configuration file and performs the parsing
  36.         and control transfer to routines which perform the desired action.
  37.     */
  38.  
  39.     /* Allocate some memory for the line buffer */
  40.     if ((buf = malloc(256)) == NULL)   {
  41.         perror(__FILE__) ;
  42.         exit(1) ;
  43.     }
  44.  
  45.     /* Read and categorize a token from the configuration file */
  46.     line = 0 ;
  47.     while (fgets(buf, 256, config_file) != NULL)   {
  48.         line++ ;
  49.         /* Extract the first token (read the next line if none is found */
  50.         if ((tok = strtok(buf, " \t\n")) == NULL)
  51.             continue ;
  52.  
  53.         if (*tok == ';')
  54.             continue ;
  55.  
  56.         for (i = 0; i < dim(cfg_cmds); i++)   {
  57.             if (stricmp(cfg_cmds[i].cmd, tok) == 0)   {
  58.                 error = (*cfg_cmds[i].command)() ;
  59.                 break ;
  60.             }
  61.         }
  62.  
  63.         if (i == dim(cfg_cmds))   {
  64.             fprintf(stderr, "Illegal input on line %d\n", line) ;
  65.             exit(1) ;
  66.         }
  67.     }
  68.     free(buf) ;
  69.     return    error ;
  70. }
  71.  
  72.  
  73. int    process_class_keyword()
  74. {
  75.     char    *tok, name[32], *p ;
  76.     unsigned int    seg ;
  77.  
  78.     /*
  79.         This function parses the remainder of the CLASS directive.
  80.     */
  81.  
  82.     /* Read the class name */
  83.     if ((tok = strtok(NULL, " \t\n")) == NULL)   {
  84.         fprintf(stderr, "Line %d: Class name expected\n", line) ;
  85.         return    ERROR ;
  86.     }
  87.     strcpy(name, strupr(tok)) ;
  88.     
  89.     /* Attempt to read the next token */
  90.     if ((tok = strtok(NULL, " \t\n")) == NULL)   {
  91.         fprintf(stderr, "Line %d: \"=\" expected\n", line) ;
  92.         return    ERROR ;
  93.     }
  94.  
  95.     /* Verify that an equal sign is present */
  96.     if (strcmp(tok, "=") != 0)   {
  97.         fprintf(stderr, "Line %d: \"=\" expected\n", line) ;
  98.         return    ERROR ;
  99.     }
  100.  
  101.     /* Read the segment number for the class */
  102.     if ((tok = strtok(NULL, " \t\n")) == NULL)   {
  103.         fprintf(stderr, "Line %d: Segment number expected\n", line) ;
  104.         return    ERROR ;
  105.     }
  106.  
  107.     seg = (unsigned int) strtol(tok, &p, 0) ;
  108.     if (*p != '\0')   {
  109.         fprintf(stderr, "Line %d: Segment number expected\n", line) ;
  110.         return    ERROR ;
  111.     }
  112.  
  113.     /* Assign the physical segment number to the specified class */
  114.     if (assign_physical_segment(name, seg) == ERROR)   {
  115.         fprintf(stderr, "Line %d: Undefined class <%s>\n", line, name) ;
  116.         return    ERROR;
  117.     }
  118.  
  119.     return    OK ;
  120. }
  121.  
  122.  
  123. int    process_order_keyword()
  124. {
  125.     char    *tok, pclass[32], class[32] ;
  126.     unsigned int    next_seg, count = 0 ;
  127.  
  128.  
  129.     /*
  130.         This function processes the ORDER directive.
  131.     */
  132.  
  133.     /* Read the leading class name from the command */
  134.     if ((tok = strtok(NULL, " \t\n")) == NULL)   {
  135.         fprintf(stderr, "Line %d: Class name expected\n", line) ;
  136.         return    ERROR ;
  137.     }
  138.     strcpy(pclass, strupr(tok)) ;
  139.  
  140.     /* Process the remaining class names in the command */
  141.     while ((tok = strtok(NULL, " \t\n")) != NULL)   {
  142.         if (*tok == ';')
  143.             break ;
  144.  
  145.         strcpy(class, strupr(tok)) ;
  146.         count++ ;
  147.  
  148.         /* Compute the segment address for this class to be made contiguous
  149.             with the previous class */
  150.         if (get_next_segment(pclass, class, &next_seg) == ERROR)   {
  151.             fprintf(stderr, "Line %d: Undefined class\n", line) ;
  152.             return    ERROR ;
  153.         }
  154.  
  155.         /* Assign the computed segment number to the class */
  156.         if (assign_physical_segment(class, next_seg) == ERROR)   {
  157.             fprintf(stderr, "Line %d: Undefined class <%s>\n", line, class) ;
  158.             return    ERROR ;
  159.         }
  160.  
  161.         /* Setup to process the next class */
  162.         strcpy(pclass, class) ;
  163.     }
  164.  
  165.     if (count == 0)   {
  166.         fprintf(stderr, "Line %d: Missing class\n", line) ;
  167.         return    ERROR ;
  168.     }
  169.  
  170.     return    OK ;
  171. }
  172.  
  173.  
  174. int    process_dup_keyword()
  175. {
  176.     char    *tok, old_class[32], new_class[32] ;
  177.  
  178.  
  179.     /*
  180.         This function is responsible for processing the DUP directive.
  181.     */
  182.  
  183.     /* Read the existing class name */
  184.     if ((tok = strtok(NULL, " \t\n")) == NULL)   {
  185.         fprintf(stderr, "Line %d: Missing class\n", line) ;
  186.         return    ERROR ;
  187.     }
  188.     strcpy(old_class, strupr(tok)) ;
  189.  
  190.     /* Read the name of the class to be created */
  191.     if ((tok = strtok(NULL, " \t\n")) == NULL)   {
  192.         fprintf(stderr, "Line %d: Missing class\n", line) ;
  193.         return    ERROR ;
  194.     }
  195.     if (*tok == ';')   {
  196.         fprintf(stderr, "Line %d: Missing class\n", line) ;
  197.         return    ERROR ;
  198.     }
  199.  
  200.     strcpy(new_class, strupr(tok)) ;
  201.  
  202.     /* Duplicate the existing class */
  203.     if (dup_class(old_class, new_class) == ERROR)   {
  204.         fprintf(stderr, "Line %d: Undefined class <%s>\n", line, old_class) ;
  205.         return    ERROR ;
  206.     }
  207.  
  208.     return    OK ;
  209. }
  210.  
  211.  
  212. int    process_rom_keyword()
  213. {
  214.     unsigned int    count = 0 ;
  215.     char    *tok, class[32] ;
  216.  
  217.     /*
  218.         This function processes the ROM keyword and marks all specified
  219.         classes as ROMable.
  220.     */
  221.  
  222.     /* Read all of the tokens on the line */
  223.     while ((tok = strtok(NULL, " \t\n")) != NULL)   {
  224.         if (*tok == ';')
  225.             break ;
  226.  
  227.         strcpy(class, strupr(tok)) ;
  228.  
  229.         if (rom_class(class) == ERROR)   {
  230.         fprintf(stderr, "Line %d: Undefined class <%s>\n", line, class) ;
  231.             return    ERROR ;
  232.         }
  233.  
  234.         count++ ;
  235.     }
  236.  
  237.     /* Flag the case of no classes specified following the directive */
  238.     if (count == 0)   {
  239.         fprintf(stderr, "Line %d: Missing class\n", line) ;
  240.         return    ERROR ;
  241.     }
  242.  
  243.     return    OK ;
  244. }
  245.